<?php
namespace Admin\Controller;
use Think\Controller;
class UploadController extends Controller{
    public function upload(){

//echo $_FILES['0']['size'];
//Array ( [goods_img] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )
             if($_FILES['goods_img1']['size']>0){
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =      __ROOT__.'Uploads/'; // 设置附件上传根目录
                // 上传单个文件
                //print_r(__ROOT__);
                $info   =   $upload->uploadOne($_FILES['goods_img1']);
                // echo $upload->ext1 . '<br />';
                // var_dump($_FILES['goods_img1']);
                // var_dump($info);
                //exit();
                //print_r($info);
                $arr['error'] = '';
                if(!$info) {// 上传错误提示错误信息
                     //$this->error($upload->getError());
                     $arr['error'] = $upload->getError();
                     echo json_encode($arr);
                }else{// 上传成功 获取上传文件信息
                     $path = $upload->rootPath.$info['savepath'].$info['savename'];
                     $image = new \Think\Image();
                     $image->open($path);
                     $spath300 = $upload->rootPath.$info['savepath'].'300'.$info['savename'];//缩略图地址;
                     //$image = new \Think\Image(\Think\Image::IMAGE_GD,'./Uploads/'.$path);
                     $image->thumb(300, 300,\Think\Image::IMAGE_THUMB_FILLED)->save($spath300);

                     $spath100 = $upload->rootPath.$info['savepath'].'100'.$info['savename'];//缩略图地址;
                     //$image = new \Think\Image(\Think\Image::IMAGE_GD,'./Uploads/'.$path);
                     $image->thumb(100, 100,\Think\Image::IMAGE_THUMB_FILLED)->save($spath100);
                     //print_r($image);
                     $arr['path']='/'.$path;
                     $arr['spath300']='/'.$spath300;
                     $arr['spath100']='/'.$spath100;
                     echo json_encode($arr);
               }
            }else{
                echo 'hello';
            }
    }
}
?>
